package com.sdry.service.impl.llm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.DepartmentMapper;
import com.sdry.mapper.llm.EmployeeMapper;
import com.sdry.mapper.llm.UserMapper;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.User;
import com.sdry.service.llm.DepartmentService;
import com.sdry.service.llm.EmployeeService;

/** 
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:53:23
*/

@Service
public class DepartmentServiceImp implements DepartmentService{

	@Resource
	private DepartmentMapper depMapper; //注入部门mapper
	@Resource
	private EmployeeMapper empMapper; //注入员工管理mapper
	@Resource
	private UserMapper userMapper; //注入用户mapper
	@Resource
	private EmployeeService employeeService; //注入员工service
	
	/**
	 * 获取部门总数
	 * @param dep 部门实体类
	 */
	@Override
	public int countDep(Department dep) {
		return depMapper.countDep(dep);
	}

	/**
	 * 分页查询部门信息
	 * @param map 查询条件集合
	 */
	@Override
	public List<Department> listPageDep(Map<String, Object> map) {
		List<Department> list = depMapper.listPageDep(map);
		for(Department dep : list){
			if(null != dep.getUid()){
				User user = userMapper.findUserByUid(dep.getUid());
				if(null != user){
					dep.setDepCharge(user.getUserName());
				}
			}
		}
		return list;
	}

	/**
	 * 新增部门
	 * @param dep 部门实体类
	 */
	@Override
	public int insertDep(Department dep) {
		int isSuccess = -2;
		//添加创建日期
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		dep.setCreateTime(time);
		List<Department> list = depMapper.getDepartmentByDname2(dep.getName());
		if(list.size() == 0){
			int i = depMapper.insertDep(dep);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改部门
	 * @param dep 部门实体类
	 */
	@Override
	public int updateDep(Department dep) {
		int isSuccess = -3;
		Department department = depMapper.getDepartmentByDid(dep.getDid());
		if(dep.getName().equals(department.getName())){
			int i = depMapper.updateDep(dep);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<Department> list = depMapper.getDepartmentByDname2(dep.getName());
			if(list.size() == 0){
				int i = depMapper.updateDep(dep);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除部门
	 * @param str 部门ID组成的字符串
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteDep(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String did : split) {
				isSuccess = depMapper.deleteDep(Long.parseLong(did));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 获取所有部门
	 */
	@Override
	public List<Department> listAllDepartment() {
		return depMapper.listAllDepartment();
	}

	/**
	 * 查询部门员工数量
	 * @param dep
	 * @return
	 */
	@Override
	public int countUserByDep(Department dep) {
		return depMapper.countUserByDep(dep);
	}

	/**
	 * 分页查询部门员工的数据
	 * @param map
	 * @return
	 */
	@Override
	public List<User> listPageUserByDepartment(Map<String, Object> map) {
		return depMapper.listPageUserByDepartment(map);
	}

	/**
	 * 删除部门员工数据在员工管理表中
	 * @param emp
	 * @return
	 */
	@Override
	public int deleteUserInEmp(Employee emp) {
		return depMapper.deleteUserInEmp(emp);
	}

	/**
	 * 校验是否删除部门
	 * @param str
	 * @return
	 */
	@Override
	public int deleteDepOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<User> userList = userMapper.getDepartmentByDidOnUser(id);
			if(userList.size() > 0 ){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}

	/**
	 *  根据ID查询部门
	 * @param parseLong
	 * @return
	 */
	@Override
	public Department getDepartmentByDid(long parseLong) {
		return depMapper.getDepartmentByDid(parseLong);
	}
}
